package com.bookmanger.server.controller;

import com.bookmanger.common.result.Result;
import com.bookmanger.pojo.Entity.PunishmentEntity;
import com.bookmanger.pojo.VO.PunishmentVO;
import com.bookmanger.server.service.FineService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/fine")
public class FineController {
    
    @Autowired
    private FineService fineService;

    /**
     * 新增：记录一笔新的罚款
     * @param punishmentEntity 罚款实体
     * @return 记录结果
     */
    @PostMapping("/record")
    public Result recordFine(@RequestBody PunishmentEntity punishmentEntity) {
        log.info("记录罚款: {}", punishmentEntity);
        boolean success = fineService.recordFine(punishmentEntity);
        return success ? Result.success("罚款记录成功") : Result.error("罚款记录失败");
    }

    @PostMapping("/pay/{id}")
    public Result payFine(@PathVariable Integer id) {
        log.info("处理罚款支付，罚款ID: {}", id);
        boolean success = fineService.payFine(id);
        if (success) {
            return Result.success("罚款支付成功");
        } else {
            return Result.error("罚款支付失败");
        }
    }
    
    @GetMapping("/reader/{readerId}")
    public Result getFinesByReaderId(@PathVariable Integer readerId) {
        log.info("查询读者未支付的罚款记录，读者ID: {}", readerId);
        return Result.success(fineService.getFinesByReaderId(readerId));
    }
    
    @GetMapping("/all")
    public Result getAllFines(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询所有罚款记录，页码: {}，每页大小: {}", page, pageSize);
        // 参数校验
        if (page <= 0) page = 1;
        if (pageSize <= 0 || pageSize > 100) pageSize = 10;
        
        PageInfo<PunishmentEntity> fines = fineService.getAllFines(page, pageSize);
        return Result.success(fines);
    }
    
    @GetMapping("/details")
    public Result getAllFinesWithReaderDetails(@RequestParam(defaultValue = "1") Integer page,
                                              @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询所有罚款记录详情（包含读者信息），页码: {}，每页大小: {}", page, pageSize);
        // 参数校验
        if (page <= 0) page = 1;
        if (pageSize <= 0 || pageSize > 100) pageSize = 10;
        
        PageInfo<PunishmentVO> fines = fineService.getAllFinesWithReaderDetails(page, pageSize);
        return Result.success(fines);
    }
    
    /**
     * 处理丢失图书的罚款
     * @param readerId 读者ID
     * @param isbnId ISBN ID
     * @param managerId 管理员ID
     * @return 处理结果
     */
    @PostMapping("/loss-book")
    public Result recordLossBookFine(@RequestParam Integer readerId, 
                                    @RequestParam Integer isbnId, 
                                    @RequestParam Integer managerId) {
        log.info("处理丢失图书罚款，读者ID: {}，ISBN ID: {}，管理员ID: {}", readerId, isbnId, managerId);
        boolean success = fineService.recordLossBookFine(readerId, isbnId, managerId);
        if (success) {
            return Result.success("丢失图书罚款记录成功");
        } else {
            return Result.error("丢失图书罚款记录失败");
        }
    }
    
    /**
     * 根据读者ID或姓名查询罚款记录
     * @param keyword 读者ID或姓名关键字
     * @param page 页码
     * @param pageSize 每页大小
     * @return 罚款记录列表
     */
    @GetMapping("/search")
    public Result getFinesByReaderIdOrName(@RequestParam String keyword,
                                          @RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("根据关键字查询罚款记录，关键字: {}，页码: {}，每页大小: {}", keyword, page, pageSize);
        // 参数校验
        if (page <= 0) page = 1;
        if (pageSize <= 0 || pageSize > 100) pageSize = 10;
        
        PageInfo<PunishmentVO> fines = fineService.getFinesByReaderIdOrName(keyword, page, pageSize);
        return Result.success(fines);
    }
}